# Maintainer: Guillaume ALAUX <guillaume@archlinux.org>
pkgname=tomcat7
pkgver=7.0.82
pkgrel=1
pkgdesc='Open source implementation of the Java Servlet 3.0 and JavaServer Pages 2.2 technologies'
arch=('any')
url='http://tomcat.apache.org/'
license=('APACHE')
depends=('java-runtime>=6' 'java-jsvc' 'java-commons-daemon' 'eclipse-ecj')
optdepends=('tomcat-native: to allow optimal performance in production environments')
backup=(etc/${pkgname}/catalina.policy
        etc/${pkgname}/catalina.properties
        etc/${pkgname}/context.xml
        etc/${pkgname}/logging.properties
        etc/${pkgname}/server.xml
        etc/${pkgname}/tomcat-users.xml
        etc/${pkgname}/web.xml)
install=${pkgname}.install
source=(http://archive.apache.org/dist/tomcat/tomcat-7/v${pkgver}/bin/apache-tomcat-${pkgver}.tar.gz{,.asc}
        systemd_${pkgname}.service
        systemd_sysusers.d_${pkgname}.conf
        systemd_tmpfiles.d_${pkgname}.conf)
validpgpkeys=('DCFD35E0BF8CA7344752DE8B6FB21E8933C60243'  # Mark E D Thomas
              '713DA88BE50911535FE716F5208B0AB1D63011C7') # Violeta Georgieva Georgieva

sha256sums=('2f19ca3fd578b8d04e72a7fd20b43beddbcece2c49eb3472265465506261676d'
            'SKIP'
            '51f599e519095ec146f5eba1a0d471117070f5b3089715bc9e392d537deb38ce'
            '4c0e12c45b406d2e8f0081de3d60dbf34c31f4da36296cedce6af5d6e03c8708'
            'bc8ce84b3b2c938f55980cbe9bc1c2a5f371e23b34cf11c1b56d2c49836761d6')

_gid_log=19
_gid_tomcat=71
_uid_tomcat=71

package() {
  cd "${srcdir}/apache-tomcat-${pkgver}"

  # Tomcat general files
  install -dm755 "${pkgdir}"/usr/share/{,java/}${pkgname}
  cp -r bin "${pkgdir}"/usr/share/${pkgname}
  # commons-daemon and tomcat-natives are packaged on their own
  rm "${pkgdir}"/usr/share/${pkgname}/bin/{*.bat,commons-daemon*,tomcat-native.tar.gz}
  ln -s /usr/share/java/commons-daemon.jar "${pkgdir}"/usr/share/${pkgname}/bin/commons-daemon.jar

  install -m644 lib/* "${pkgdir}"/usr/share/java/${pkgname}
  # eclipse-ecj is packaged on its own
  rm "${pkgdir}"/usr/share/java/${pkgname}/ecj-*.jar
  ln -s ../eclipse-ecj.jar "${pkgdir}"/usr/share/java/${pkgname}/ecj.jar

  ln -s /usr/share/java/${pkgname} "${pkgdir}"/usr/share/${pkgname}/lib

  # We log through systemd but this would still be required for stock Tomcat logging
  install -dm775 -o ${_uid_tomcat} -g ${_gid_log} "${pkgdir}"/var/log/${pkgname}
  ln -s /var/log/${pkgname} "${pkgdir}"/usr/share/${pkgname}/logs
  touch "${pkgdir}"/var/log/${pkgname}/catalina.{out,err}
  chgrp ${_gid_log} "${pkgdir}"/var/log/${pkgname}/catalina.{out,err}

  install -dm775 "${pkgdir}"/etc/${pkgname}
  install -g ${_gid_tomcat} -m640 conf/* "${pkgdir}"/etc/${pkgname}
  install -d -g ${_gid_tomcat} -m775 "${pkgdir}"/etc/${pkgname}/Catalina
  ln -s /etc/${pkgname} "${pkgdir}"/usr/share/${pkgname}/conf

  install -dm775 "${pkgdir}"/var/lib/${pkgname}
  cp -r webapps "${pkgdir}"/var/lib/${pkgname}
  chown -R ${_uid_tomcat}:${_gid_tomcat} "${pkgdir}"/var/lib/${pkgname}
  chmod 775 "${pkgdir}"/var/lib/${pkgname}/webapps
  ln -s /var/lib/${pkgname}/webapps "${pkgdir}"/usr/share/${pkgname}/webapps

  install -dm1777 "${pkgdir}"/var/tmp
  install -dm775 -o ${_uid_tomcat} -g ${_gid_tomcat} "${pkgdir}"/var/tmp/${pkgname}/{temp,work}
  ln -s /var/tmp/${pkgname}/temp "${pkgdir}"/usr/share/${pkgname}/temp
  ln -s /var/tmp/${pkgname}/work "${pkgdir}"/usr/share/${pkgname}/work

  install -Dm644 "${srcdir}"/systemd_${pkgname}.service \
                 "${pkgdir}"/usr/lib/systemd/system/${pkgname}.service
  install -Dm644 "${srcdir}"/systemd_sysusers.d_${pkgname}.conf \
                 "${pkgdir}"/usr/lib/sysusers.d/${pkgname}.conf
  install -Dm644 "${srcdir}"/systemd_tmpfiles.d_${pkgname}.conf \
                 "${pkgdir}"/usr/lib/tmpfiles.d/${pkgname}.conf
}
